use proc_macro::TokenStream;
use quote::quote;


pub fn impl_curd_entry(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let field_idents = get_fields_name(&ast.data);
    if !field_idents.contains(&"id".to_string()) || !field_idents.contains(&"delete_flag".to_string()) {
        panic!("id 和 delete_flag 是必须的");
    }
    let qt = quote! {
        #ast

        impl DAOBase for #name {
            fn get_id(&self) -> &Option<Uuid> {
                &self.id
            }
        }

        impl IntoResponse for #name {
            fn into_response(self) -> Response {
                CusResponse::ok(self).into_response()
            }
        }
    };
    qt.into()
}

fn get_fields_name(data: &syn::Data) -> Vec<String> {
    let mut fields = vec![];
    match &data {
        syn::Data::Struct(s) => {
            for field in &s.fields {
                match &field.ident {
                    None => {}
                    Some(v) => {
                        fields.push(v.to_string().trim_start_matches("r#").to_string());
                    }
                }
            }
        }
        _ => {
            panic!("[axum_test] #[curd_entry] only support struct for curd_entry's macro!")
        }
    }
    fields
}